package com.gitee.shigen.oss.controller;

import cn.hutool.core.io.FileUtil;
import com.gitee.shigen.common.exception.BaseException;
import com.gitee.shigen.common.msg.Result;
import com.gitee.shigen.oss.cloud.OSSFactory;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


/**
 * 文件上传
 *
 * @author shigenfu
 */
@RestController
@RequestMapping("/oss")
public class OssController {

    @Resource
    private OSSFactory ossFactory;

    /**
     * 上传文件
     *
     * @return 上传地址
     */
    @RequestMapping("/upload")
    public Result<String> upload(@RequestParam("file") MultipartFile file) throws Exception {
        if (file.isEmpty()) {
            throw new BaseException("上传文件不能为空");
        }
        //上传文件的后缀
        String suffix = FileUtil.getSuffix(file.getOriginalFilename());
        String url = ossFactory.build().uploadSuffix(file.getBytes(), suffix);
        return Result.success(url);
    }


}
